﻿
@{
    Layout = null;
}

<div>
    <div class="input-group">
        <div class="col-3 pr-0" style="display: none">
            <label id="SAMPLE-AddSampleDefinition-SampleDefinitionId">@ViewData["SampleDefinitionId"]</label>
            <label id="SAMPLE-AddSampleDefinition-Parent-ModalID">@ViewData["ModalID"]</label>
        </div>
    </div>
    <div class="input-group">
        <div class="col-3 pr-0">
            <span class="input-group-text font-rfs border-radius-noright noRightBorder">名称*</span>
        </div>
        <div class="col-9 pl-0">
            <input id="SAMPLE-AddSampleDefinition-Name" class="form-control font-rfs border-radius-noleft" placeholder="必填" />
        </div>
    </div>
    <div class="input-group">
        <div class="col-3 pr-0">
            <span class="input-group-text font-rfs border-radius-noright noRightBorder">样品类型*</span>
        </div>
        <div class="col-9 pl-0 input-group position-relative">
            <select id="SAMPLE-AddSampleDefinition-Select-Classification" class="form-control font-rfs border-radius-noleft" style="width:90%"></select>
            <div class="col-11 pl-0 input-group position-absolute float-left"><input id="SAMPLE-AddSampleDefinition-Input-Classification" class="form-control font-rfs border-radius-noleft noRightBorder" style="width:100%" placeholder="可输入新的类型" /></div>
        </div>
    </div>
    <div class="input-group">
        <div class="col-3 pr-0">
            <span class="input-group-text font-rfs border-radius-noright noRightBorder">默认编号</span>
        </div>
        <div class="col-9 pl-0">
            <input id="SAMPLE-AddSampleDefinition-DefaultNumber" class="form-control font-rfs border-radius-noleft" placeholder="可选" />
        </div>
    </div>
    <div class="input-group">
        <div class="col-3 pr-0">
            <span class="input-group-text font-rfs border-radius-noright noRightBorder">样品分类</span>
        </div>
        <div class="col-9 pl-0 input-group position-relative">
            <select id="SAMPLE-AddSampleDefinition-Select-GenricClass" class="form-control font-rfs border-radius-noleft" style="width:90%"></select>
            <div class="col-11 pl-0 input-group position-absolute float-left"><input id="SAMPLE-AddSampleDefinition-Input-GenricClass" class="form-control font-rfs border-radius-noleft noRightBorder" style="width:100%" placeholder="可输入新的类型" /></div>
        </div>
    </div>
    <div class="input-group">
        <div class="col-3 pr-0">
            <span class="input-group-text font-rfs border-radius-noright noRightBorder">实验室和组</span>
        </div>
        <div class="col-9 pl-0 input-group">
            <select id="SAMPLE-AddSampleDefinition-Site" class="form-control font-rfs border-radius-noleft" style="width:50%"></select>
            <select id="SAMPLE-AddSampleDefinition-Group" class="form-control font-rfs border-radius-noleft" style="width:50%"></select>
        </div>
    </div>
    <div class="input-group">
        <div class="col-3 pr-0">
            <span class="input-group-text font-rfs border-radius-noright noRightBorder">关联流程</span>
        </div>
        <div class="col-9 pl-0 input-group">
            <select id="SAMPLE-AddSampleDefinition-WorkFlow" class="form-control font-rfs border-radius-noleft" style="width:90%"></select>
        </div>
    </div>
    <div class="input-group">
        <div class="col-3 pr-0">
            <span class="input-group-text font-rfs border-radius-noright noRightBorder">关联报告</span>
        </div>
        <div class="col-9 pl-0 input-group">
            <select id="SAMPLE-AddSampleDefinition-Report" class="form-control font-rfs border-radius-noleft" style="width:90%"></select>
        </div>
    </div>
    <div class="input-group">
        <div class="col-3 pr-0">
            <span class="input-group-text font-rfs border-radius-noright noRightBorder">采样点</span>
        </div>
        <div class="col-9 pl-0 input-group position-relative">
            <select id="SAMPLE-AddSampleDefinition-SamplingPoint" class="form-control font-rfs border-radius-noleft"></select>
        </div>
    </div>
    <div class="input-group">
        <div class="col-3 pr-0">
            <span class="input-group-text font-rfs border-radius-noright noRightBorder">描述</span>
        </div>
        <div class="col-9 pl-0">
            <input id="SAMPLE-AddSampleDefinition-Description" class="form-control font-rfs border-radius-noleft" placeholder="可选" />
        </div>
    </div>
    <br />
    <div class="text-right">
        <button id="SAMPLE-AddSampleDefinition-confirm" class="btn btn-primary font-rfs">确定</button>
        <button id="SAMPLE-AddSampleDefinition-cancel" type="button" class="btn btn-info font-rfs">取消</button>
    </div>
</div>

<script>
    var AddSampleDefinition_Id = $("#SAMPLE-AddSampleDefinition-SampleDefinitionId").html();

    /*初始化页面控件值，加载样品类型*/
    $(document).ready(function() { 
        LoadSampleDefinitionClassification();
        LoadSiteList('SAMPLE-AddSampleDefinition-Site');
        LoadWorkFlowByType('SAMPLE-AddSampleDefinition-WorkFlow', "检测");
        LoadReport();
        GetLocationList('SAMPLE-AddSampleDefinition-SamplingPoint', '采样点');

        setTimeout(function () {
            if (AddSampleDefinition_Id.length != 0) {
                // 如果是编辑状态，初始化页面控件值
                $.post("GetSampleDefinition", {
                    Id: AddSampleDefinition_Id
                }, function (data) {
                    $("#SAMPLE-AddSampleDefinition-Name").val(data.name); /*这个地方属性首字母都是小写，否则数据绑定不上*/
                    $("#SAMPLE-AddSampleDefinition-DefaultNumber").val(data.defaultNumber);
                    $("#SAMPLE-AddSampleDefinition-Input-Classification").val(data.classification);
                    $("#SAMPLE-AddSampleDefinition-Input-GenricClass").val(data.genricClass);
                    $("#SAMPLE-AddSampleDefinition-Site").val(data.site);
                    LoadGroupList(data.site, 'SAMPLE-AddSampleDefinition-Group', data.group);
                    $("#SAMPLE-AddSampleDefinition-Description").val(data.description);
                    $("#SAMPLE-AddSampleDefinition-WorkFlow").find("option:contains(" + data.workFlowName + ")").attr("selected", true);
                    $("#SAMPLE-AddSampleDefinition-Report").find("option:contains(" + data.reportName + ")").attr("selected", true);
                    $("#SAMPLE-AddSampleDefinition-SamplingPoint").val(data.samplingPoint);
                });
            }
        }, 1000);
    });

    /*加载样品类型*/
    function LoadSampleDefinitionClassification() {
        $.post("GetSampleDefinitionClassification", {
            //Type: type  // 传递的参数，对应控制器里的参数
        }, function (data) {
            for (var i = 0; i < data.length; i++) {
                $("#SAMPLE-AddSampleDefinition-Select-Classification").append(new Option(data[i].classification));
            }
            $("#SAMPLE-AddSampleDefinition-Select-Classification").prepend(new Option("", ""));
            $("#SAMPLE-AddSampleDefinition-Select-Classification").val("");
        });
    }

    function LoadReport() {
        $.post("GetReportList", {
            //Type: type  // 传递的参数，对应控制器里的参数
        }, function (data) {
            for (var i = 0; i < data.length; i++) {
                $("#SAMPLE-AddSampleDefinition-Report").append(new Option(data[i]));
            }
            $('#SAMPLE-AddSampleDefinition-Report').prepend(new Option(""));
            $("#SAMPLE-AddSampleDefinition-Report").find("option:contains(data[i])").attr("selected", true);
        });
    }

    //根据选择的实验室加载工作组
    $("#SAMPLE-AddSampleDefinition-Site").change(function () {
        var site = $("#SAMPLE-AddSampleDefinition-Site").find("option:selected").text();
        LoadGroupList(site, 'SAMPLE-AddSampleDefinition-Group');
    });

    $("#SAMPLE-AddSampleDefinition-Select-Classification").change(function () {
        $("#SAMPLE-AddSampleDefinition-Input-Classification").val($("#SAMPLE-AddSampleDefinition-Select-Classification").find("option:selected").text());
        $("#SAMPLE-AddSampleDefinition-Input-GenricClass").val('');

        $.post("GetSampleDefinitionGenricClass", { /* 加载 样品分类 信息*/
            Classification: $("#SAMPLE-AddSampleDefinition-Select-Classification").find("option:selected").text()  // 传递的参数，对应控制器里的参数
        }, function (data) {
            $("#SAMPLE-AddSampleDefinition-Select-GenricClass").empty();
            for (var i = 0; i < data.length; i++) {
                $("#SAMPLE-AddSampleDefinition-Select-GenricClass").append(new Option(data[i].genricClass));
            }
            $("#SAMPLE-AddSampleDefinition-Select-GenricClass").prepend(new Option("", ""));
            $("#SAMPLE-AddSampleDefinition-Select-GenricClass").val("");
        });
    });

    $("#SAMPLE-AddSampleDefinition-Select-GenricClass").change(function () {
        $("#SAMPLE-AddSampleDefinition-Input-GenricClass").val($("#SAMPLE-AddSampleDefinition-Select-GenricClass").find("option:selected").text());
    });

    $("#SAMPLE-AddSampleDefinition-confirm").click(function () {
        if ($("#SAMPLE-AddSampleDefinition-Name").val().length == 0) {
            alert("样品名称不能为空！");
            $("#SAMPLE-AddSampleDefinition-Name").addClass('error-style');
            return false;
        }
        if ($("#SAMPLE-AddSampleDefinition-Input-Classification").val().length == 0) {
            alert("样品类型不能为空！");
            $("#SAMPLE-AddSampleDefinition-Input-Classification").addClass('error-style');
            return false;
        }
        if ($("#SAMPLE-AddSampleDefinition-Site").find("option:selected").text().length == 0) {
            alert("样品所属实验室不能为空！");
            $("#SAMPLE-AddSampleDefinition-Site").addClass('error-style');
            return false;
        }
        if ($("#SAMPLE-AddSampleDefinition-Group").find("option:selected").text().length == 0) {
            alert("样品所属工作组不能为空！");
            $("#SAMPLE-AddSampleDefinition-Group").addClass('error-style');
            return false;
        }

        var jsonStr = {};
        jsonStr["Name"] = $("#SAMPLE-AddSampleDefinition-Name").val();
        jsonStr["DefaultNumber"] = $("#SAMPLE-AddSampleDefinition-DefaultNumber").val();
        jsonStr["Classification"] = $("#SAMPLE-AddSampleDefinition-Input-Classification").val();
        jsonStr["GenricClass"] = $("#SAMPLE-AddSampleDefinition-Input-GenricClass").val();
        jsonStr["Description"] = $("#SAMPLE-AddSampleDefinition-Description").val();
        jsonStr["Site"] = $("#SAMPLE-AddSampleDefinition-Site").find("option:selected").text();
        jsonStr["Group"] = $("#SAMPLE-AddSampleDefinition-Group").find("option:selected").text();
        jsonStr["WorkFlowId"] = $("#SAMPLE-AddSampleDefinition-WorkFlow").val();
        jsonStr["WorkFlowName"] = $("#SAMPLE-AddSampleDefinition-WorkFlow").find("option:selected").text();
        jsonStr["ReportName"] = $("#SAMPLE-AddSampleDefinition-Report").find("option:selected").text();
        jsonStr["SamplingPoint"] = $("#SAMPLE-AddSampleDefinition-SamplingPoint").find("option:selected").text();

        if (AddSampleDefinition_Id.length == 0) {
            $.post("postAddSampleDefinition", {
                jsonStr: JSON.stringify(jsonStr)
            }, function(data) {
                var modalID = $("#SAMPLE-AddSampleDefinition-Parent-ModalID").html(); // 这两行代码不能提到最后面，否则因为异步会造成刷新不到数据。
                $("#" + modalID).modal('hide');
            });
        } else {
            jsonStr["Id"] = AddSampleDefinition_Id;
            $.post("postUpdateSampleDefinition", {
                jsonStr: JSON.stringify(jsonStr)
            }, function(data) {
                var modalID = $("#SAMPLE-AddSampleDefinition-Parent-ModalID").html();
                $("#" + modalID).modal('hide');
            });
        }

    });

    $("#SAMPLE-AddSampleDefinition-cancel").click(function () {
        var modalID = $("#SAMPLE-AddSampleDefinition-Parent-ModalID").html();
        $("#" + modalID).modal('hide');
    });
    
</script>
